package com.xiyu.service.repository.market.setting;

import com.xiyu.service.model.market.setting.*;
import com.xiyu.service.model.system.user.SystemUser;
import com.xiyu.service.model.system.user.SystemUserFetcher;
import com.xiyu.service.vo.market.marketSetting.MarketSettingSenderListInput;
import org.babyfish.jimmer.spring.repository.JRepository;

import java.util.Optional;
import java.util.List;

public interface MarketSettingSenderRepository extends JRepository<MarketSettingSender, Long> {
    MarketSettingSenderTable marketSettingSenderTable = MarketSettingSenderTable.$;

    default List<MarketSettingSender> listQuery(MarketSettingSenderListInput inputVO, List<Long> ownerUserId){
        return sql().createQuery(marketSettingSenderTable)
                .where(marketSettingSenderTable.senderAddress().likeIf(inputVO.getSenderAddress()))
                .where(marketSettingSenderTable.senderName().likeIf(inputVO.getSenderName()))
                .where(marketSettingSenderTable.reply().replyAddress().likeIf(inputVO.getReplyAddress()))
                .where(marketSettingSenderTable.ownerId().in(ownerUserId))
                .orderBy(marketSettingSenderTable.id().desc())
                .select(marketSettingSenderTable.fetch(MarketSettingSenderFetcher.$.allTableFields()
                        .reply(MarketSettingReplyFetcher.$.allTableFields())
                        .owner(SystemUserFetcher.$.nickname())
                        .domain(MarketSettingDomainFetcher.$.allTableFields())
                ))
                .execute();
    }

    default Optional<MarketSettingSender> findDetailById(Long id){
        return sql().createQuery(marketSettingSenderTable)
                .where(marketSettingSenderTable.id().eq(id))
                .select(marketSettingSenderTable.fetch(MarketSettingSenderFetcher.$.allTableFields()
                        .domain(MarketSettingDomainFetcher.$.allTableFields())
                        .reply(MarketSettingReplyFetcher.$.allTableFields())
                ))
                .fetchOptional();
    }

    Optional<MarketSettingSender> findBySenderAddressAndDomainIdAndOwnerId(String senderAddress, Long domainId, Long ownerId);


}